/**
 * @(#)HttpXmlRequestEncoder.java, 2018/12/27
 * <p/>
 * Copyright 2018 Netease, Inc. All rights reserved.
 * NETEASE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.chengzhi.netty.http.xml;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.*;

import static io.netty.handler.codec.http.HttpHeaderNames.*;
import static io.netty.handler.codec.http.HttpHeaderValues.*;

import java.net.InetAddress;
import java.util.List;

/**
 * @author 阮承志(wb.ruanchengzhi @ mesg.corp.netease.com)
 **/
public class HttpXmlRequestEncoder extends AbstractHttpXmlEncoder<HttpXmlRequest> {


    @Override
    protected void encode(ChannelHandlerContext ctx, HttpXmlRequest msg, List<Object> out) throws Exception {
        ByteBuf body = this.encode0(ctx, msg);
        FullHttpRequest request = msg.getRequest();
        if (null == request) {
            request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/do", body);
            HttpHeaders headers = request.headers();
            headers.set(HOST, InetAddress.getLocalHost());
            headers.set(ACCEPT_ENCODING,
                    GZIP.toString() + "," + DEFLATE.toString());
            headers.set(ACCEPT_CHARSET, "ISO-8859-1,UTF-8,q=0.7,*,q=0.7");
            headers.set(USER_AGENT, "Netty xml Http Client side");
            headers.set(ACCEPT, "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        }
        HttpHeaders.setContentLength(request, body.readableBytes());
        out.add(request);
    }
}
